{extend name='layout/layout'}
{block name='content'}
<div class="row">
    <div class="col-sm-12">
        <div class="ibox float-e-margins">
            <div class="ibox-title">
                <h5>编辑接口</h5>
            </div>
            <div class="ibox-content">
                <form class="form-horizontal m-t" id="commentForm" method="post" action="{:url('edit')}">
                    <input type="hidden" name="id" value="{$list.id}"/>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">接口名称：</label>
                        <div class="input-group col-sm-4">
                            <input id="title" type="text" value="{$list.title}" class="form-control" name="title"
                                   required=""
                                   aria-required="true">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">接口地址：</label>
                        <div class="input-group col-sm-4">
                            <input id="url" type="text" value="{$list.url}" class="form-control" name="url" required=""
                                   aria-required="true">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">请求路径：</label>
                        <div class="input-group col-sm-4">
                            <input id="path" type="text" value="{$list.path}" placeholder="版本号/控制器/方法"
                                   class="form-control" name="path" required=""
                                   aria-required="true">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">请求类型：</label>
                        <div class="input-group col-sm-4">
                            <select class="form-control" name="method" required="" aria-required="true">
                                <option value="">请选择</option>
                                {foreach name="$methodList" item="v" key='k'}
                                <option value="{$k}" {if condition="$list.method eq $k" }selected="selected" {/if}>{$v}</option>
                                {/foreach}
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">验证签名：</label>
                        <div class="input-group col-sm-4">
                            <select class="form-control" name="validate_sign" required="" aria-required="true">
                                <option value="1" {if condition="$list.validate_sign eq 1"} selected="selected" {/if}>是</option>
                                <option value="0" {if condition="$list.validate_sign eq 0"} selected="selected" {/if}>否</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">验证时间：</label>
                        <div class="input-group col-sm-4">
                            <select class="form-control" name="validate_time" required="" aria-required="true">
                                <option value="1" {if condition="$list.validate_time eq 1"} selected="selected" {/if}>是</option>
                                <option value="0" {if condition="$list.validate_time eq 0"} selected="selected" {/if}>否</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">验证user_token：</label>
                        <div class="input-group col-sm-4">
                            <select class="form-control" name="validate_u_token" required="" aria-required="true">
                                <option value="1" {if condition="$list.validate_u_token eq 1"} selected="selected" {/if} >是</option>
                                <option value="0" {if condition="$list.validate_u_token eq 0"} selected="selected" {/if}>否</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">验证access_token：</label>
                        <div class="input-group col-sm-4">
                            <select class="form-control" name="validate_a_token" required="" aria-required="true">
                                <option value="1" {if condition="$list.validate_a_token eq 1" } selected="selected" {/if}>是</option>
                                <option value="0" {if condition="$list.validate_a_token  eq 0" } selected="selected" {/if}>否</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">接口备注：</label>
                        <div class="input-group col-sm-4">
                            <input id="mark" type="text" value="{$list.mark}" class="form-control" name="mark">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">请求参数</label>
                        <div class="input-group col-sm-8">
                            <table class="table requestList col-sm-push-10">
                                <thead>
                                <tr>
                                    <th>名称</th>
                                    <th>描述</th>
                                    <th>长度</th>
                                    <th>类型</th>
                                    <th>过滤</th>
                                    <th>必须</th>
                                    <th>参数类型</th>
                                    <th>条件</th>
                                    <th>备注</th>
                                    <th>举例</th>
                                    <th>
                                        <i class="fa fa-plus btn requestAdd"></i>
                                    </th>
                                </tr>
                                </thead>
                                <tbody>
                                {php}$i=0;{/php}
                                {foreach name="$requestList" item="v" key="k"}
                                {eq name="$v.request" value="1"}
                                <tr data-index="{$i}">
                                    <input type="hidden" name="request[{$i}][id]" value="{$v.id}">
                                    <td><input type="text" name="request[{$i}][name]" value="{$v.name}"></td>
                                    <td><input type="text" name="request[{$i}][title]" value="{$v.title}"></td>
                                    <td><input type="number" style="width: 40px;" name="request[{$i}][length]"
                                               value="{$v.length}"></td>
                                    <td>
                                        <select name="request[{$i}][type]" class="form-control" required
                                                aria-required="true">
                                            {foreach name="$fieldType" item="val" key="vo"}
                                            <option value="{$vo}" {if $vo==$v.type}selected="selected"{/if}>{$val}
                                            </option>
                                            {/foreach}
                                        </select>
                                    </td>
                                    <td>
                                        <select name="request[{$i}][filter]" class="form-control" required
                                                aria-required="true">
                                            {foreach name="$encryptList" item="val" }
                                            <option value="{$val}" {if $val==$v.filter}selected="selected"{/if}>{$val}
                                            </option>
                                            {/foreach}
                                        </select>
                                    </td>
                                    <td>
                                        <select name="request[{$i}][required]" class="form-control" required
                                                aria-required="true">
                                            {foreach name="$statusList" item="vo" key="k"}
                                            <option value="{$k}" {if $k==$v.required}selected="selected"{/if}>{$vo}
                                            </option>
                                            {/foreach}
                                        </select>
                                    </td>
                                    <td>
                                        <select name="request[{$i}][is_file]" class="form-control" required
                                                aria-required="true">
                                            {foreach name="$paramType" item="vo" key="k"}
                                            <option value="{$k}" {if $k==$v.is_file}selected="selected"{/if}>{$vo}
                                            </option>
                                            {/foreach}
                                        </select>
                                    </td>
                                    <td><input type="text" name="request[{$i}][condition]" value="{$v.condition}"></td>
                                    <td><input type="text" name="request[{$i}][mark]" value="{$v.mark}"></td>
                                    <td><input type="text" name="request[{$i}][example]" value="{$v.example}"></td>
                                    <td><i class="fa fa-times btn" data-id='{$v.id}'></i></td>
                                </tr>
                                {php}$i++;{/php}
                                {/eq}
                                {/foreach}
                                </tbody>
                            </table>
                        </div>
                    </div>
                    <hr>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">响应参数</label>
                        <div class="input-group col-sm-8">
                            <table class="table responseList col-sm-push-10">
                                <thead>
                                <tr>
                                    <th>名称</th>
                                    <th>描述</th>
                                    <th>长度</th>
                                    <th>类型</th>
                                    <th>过滤</th>
                                    <th>必须</th>
                                    <th>条件</th>
                                    <th>备注</th>
                                    <th>举例</th>
                                    <th>
                                        <i class="fa fa-plus btn responseAdd"></i>
                                    </th>
                                </tr>
                                </thead>
                                <tbody>
                                {php}$m=0;{/php}
                                {foreach name="$requestList" item="v" key="k"}
                                {eq name="$v.request" value="2"}
                                <tr data-index="{$m}">
                                    <input type="hidden" name="response[{$m}][id]" value="{$v.id}">
                                    <td><input type="text" name="response[{$m}][name]" value="{$v.name}"></td>
                                    <td><input type="text" name="response[{$m}][title]" value="{$v.title}"></td>
                                    <td><input type="number" style="width: 40px;" name="response[{$m}][length]"
                                               value="{$v.length}"></td>
                                    <td>
                                        <select name="response[{$m}][type]" class="form-control" required
                                                aria-required="true">
                                            {foreach name="$fieldType" item="val" key="vo"}
                                            <option value="{$vo}" {if $vo==$v.type}selected="selected"{/if}>{$val}
                                            </option>
                                            {/foreach}
                                        </select>
                                    </td>
                                    <td>
                                        <select name="response[{$m}][filter]" class="form-control" required
                                                aria-required="true">
                                            {foreach name="$encryptList" item="val" }
                                            <option value="{$val}" {if $val==$v.filter}selected="selected"{/if}>{$val}
                                            </option>
                                            {/foreach}
                                        </select>
                                    </td>
                                    <td>
                                        <select name="response[{$m}][required]" class="form-control" required
                                                aria-required="true">
                                            {foreach name="$statusList" item="vo" key="k"}
                                            <option value="{$k}" {if $k==$v.required}selected="selected"{/if}>{$vo}
                                            </option>
                                            {/foreach}
                                        </select>
                                    </td>
                                    <td><input type="text" name="response[{$m}][condition]" value="{$v.condition}"></td>
                                    <td><input type="text" name="response[{$m}][mark]" value="{$v.mark}"></td>
                                    <td><input type="text" name="response[{$m}][example]" value="{$v.example}"></td>
                                    <td><i class="fa fa-times btn" data-id='{$v.id}'></i></td>
                                </tr>
                                {php}$m++;{/php}
                                {/eq}
                                {/foreach}
                                </tbody>
                            </table>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-4 col-sm-offset-6">
                            <button class="btn btn-primary" type="submit">提交</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>

    </div>
</div>
{/block}

{block name='js'}
<script type="text/javascript">
    function getTr(_id, type) {
        if (parseInt(_id) > 20) {
            layer.alert('参数不能超过20个');
            return false;
        }
        return '<tr data-index="' + _id + '"> ' +
            '<td><input type="text" name="' + type + '[' + _id + '][name]" style="width: 80px;"></td> ' +
            '<td><input type="text" name="' + type + '[' + _id + '][title]" style="width: 80px;"></td> ' +
            '<td><input type="number" style="width: 40px;" value="0" name="' + type + '[' + _id + '][length]"></td> ' +
            '<td> <select class="form-control"  name="' + type + '[' + _id + '][type]" aria-required="true">{foreach name="$fieldType" item="v" key="k"} <option value="{$k}">{$v}</option>{/foreach} </select> </td> ' +
            '<td> <select class="form-control" name="' + type + '[' + _id + '][filter]" aria-required="true"> <option value="">请选择</option>{foreach name="$encryptList" item="v"} <option value="{$v}">{$v}</option>{/foreach} </select> </td>' +
            '<td><select class="form-control" name="' + type + '[' + _id + '][required]" required="" aria-required="true">' +
            '{foreach name="$statusList" item="v" key="k"} ' +
            '<option value="{$k}" {if $k==1}selected="selected"{/if}>{$v}</option>' +
            '{/foreach} </select></td> ' +
            '<td><select class="form-control" name="' + type + '[' + _id + '][is_file]"><option>--请选择--</option>{foreach name="$paramType" item="v" key="k"} <option value="{$k}" {if $k==0}selected="selected"{/if}>{$v}</option>{/foreach} </select></td> ' +
            '<td><input type="text" name="' + type + '[' + _id + '][condition]"></td> ' +
            '<td><input type="text" name="' + type + '[' + _id + '][mark]"></td> ' +
            '<td><input type="text" name="' + type + '[' + _id + '][example]"></td> ' +
            '<td><i class="fa fa-times btn" onclick="$(this).parent().parent().remove()" data-id="' + _id + '"></i></td>' +
            '</tr>';
    }

    $(document).ready(function () {
        $('.requestAdd').click(function () {
            var _id = $('.requestList tbody tr:last').data('index');
            if (_id == undefined || isNaN(_id)) {
                _id = 0;
            } else {
                _id += 1;
            }
            $('.requestList tbody').append(getTr(_id, 'request'));
        });

        $('.responseAdd').click(function () {
            var _id = $('.responseList tbody tr:last').data('index');
            if (_id == undefined || isNaN(_id)) {
                _id = 0;
            } else {
                _id += 1;
            }
            $('.responseList tbody').append(getTr(_id, 'response'));
        });
    });

    $(document).on('click', '.fa-times', function () {
        var obj = $(this);
        var id = obj.data('id');
        if (id == '') {
            $(this).parent().parent().remove();
            return false;
        }
        layer.confirm('确定要删除该字段吗？', {
            btn: ['确定', '不要'] //按钮
        }, function () {
            $.post('{:url("admin/Apilist/deleteById")}', {id: id}, function (data) {
                if (data.code == 1) {
                    obj.parent().parent().remove();
                    layer.msg(data.msg, {icon: 1});
                } else {
                    layer.msg(data.msg, {icon: 2});
                }
            });
        });
    });
</script>
{/block}
